﻿using System.Web.Mvc;
using SweetSurvey.Models.Repositories;
using SweetSurvey.Models.ViewModels;
using System.Collections.Generic;
using System;
using System.Globalization;
using System.Web.Script.Serialization;
using SweetSurvey.Models;

namespace SweetSurvey.Controllers
{
    public class QuestionController : ControllerBase
    {
        IQuestionRepository repository;

        #region Constructors

        public QuestionController(IQuestionRepository repository)
        {
            this.repository = repository;
        }

        #endregion

        #region Public Methods

        //
        // POST: /Question/GetSampleQuestions

        [HttpPost]
        public ActionResult GetSampleQuestions()
        {
            var questions = this.repository.GetSampleQuestions();
            var questionsViewModels = new QuestionsViewModel(questions);
            return Json(questionsViewModels);
        }

        //
        // POST: /Question/GetQuestions/vWxYz

        [HttpPost]
        public ActionResult GetQuestions(string surveyId)
        {
            var questions = this.repository.GetQuestionsBySurveyId(surveyId);
            var questionsViewModels = new QuestionsViewModel(questions);
            return Json(questionsViewModels);
        }

        //
        // POST: /Question/Create

        [HttpPost]
        public ActionResult Create(string questionJsonString)
        {
            try
            {
                var serializer = new JavaScriptSerializer();
                var question = serializer.Deserialize<QuestionViewModel>(questionJsonString);
                question.QuestionId = RepositoryBase.GenerateRandomId();
                return Json(question);
            }
            catch
            {
                return Json(new QuestionViewModel());
            }
        }

        #endregion
    }
}
